/*
 * Created on Mar 17, 2004
 *
 * To change the template for this generated file go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
package littlecyberwar.ui;

import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import javax.swing.event.*; 
import javax.swing.*;
import javax.swing.border.*;
import java.util.*;

import littlecyberwar.tool.MouseHandler;
import littlecyberwar.util.Util;
import littlecyberwar.util.*;

import ModuleBuilder.model.*;


/**
 * @author Milton  Soong
 *
 * To change the template for this generated type comment go to
 * Window&gt;Preferences&gt;Java&gt;Code Generation&gt;Code and Comments
 */
public class ProfilePanel extends JPanel {
		
	private HashMap attributeLookup;	// from name==>Attribute spinnerModel
	private HashMap nameLookup;			// component(JSpinner)==>Name lookup
	private JLabel unitNameValueLabel = new JLabel(" ");
	
	public ProfilePanel (UnitProfileModel mod) {
		
		this.setLayout(new SpringLayout());
		
		// now loop through each pair of attribute, and then add them to the panel,
		// and also add the attribute lookup into the HashMap
		ArrayList allAttrib = mod.getAttributes();
		int numOfAttrib = allAttrib.size();
		attributeLookup = new HashMap(numOfAttrib);
		nameLookup = new HashMap(numOfAttrib);
		
		// setup the ProfileCardPanel as the change listener for ALL the spinners
		ProfileCardPanel profCardPanel = ProfileCardPanel.getInstance();
		
		// always put in the name of the unit as the first item
		JLabel unitNameLabel = new JLabel("Unit Name:");
		this.add(unitNameLabel);
		this.add(unitNameValueLabel);
		
		// first put the distance Moved into the display
		String attribName = Util._elementAttribDistanceMoved;
		Double distanceMoved = new Double(0.0);
		JLabel nameLab = new JLabel(attribName);
		JLabel vallab = new JLabel(distanceMoved.toString());
		this.add(nameLab);
		this.add(vallab);
		attributeLookup.put(attribName, vallab);

		// loop through the attributes and plants them
		for ( int i=0; i < numOfAttrib; i++ ){
			UnitProfileAttribute attrib = (UnitProfileAttribute)allAttrib.get(i);
			attribName = attrib.getName();
			Object attribVal = attrib.getVal();
			
			// depending on the type of object (could be string, number, or SpinnerModel
			// only spinner model would go into the lookup since nothing else would need to be looked up
			//
			nameLab = new JLabel(attribName);
			Dimension d = nameLab.getMinimumSize();
			nameLab.setPreferredSize(d);
			nameLab.setMaximumSize(d);
			// Debug.out("nameLab size= "+d.toString());
			
			if ( (attribVal instanceof String) || ( attribVal instanceof Integer ) ) {
				
				vallab = new JLabel(attribVal.toString());
				d = vallab.getMinimumSize();
				vallab.setPreferredSize(d);
				vallab.setMaximumSize(d);
				// Debug.out("vallab size= "+d.toString());
				this.add(nameLab);
				this.add(vallab);
				
				
			} else if ( attribVal instanceof RangeUnitProfileAttribute ){
				RangeUnitProfileAttribute in = (RangeUnitProfileAttribute)attribVal;
				SpinnerNumberModel model = new SpinnerNumberModel(in.getMaxVal(), in.getMinVal(), in.getMaxVal(), in.getStepVal());
				JSpinner spin = new JSpinner(model);
				d = spin.getMinimumSize();
				spin.setPreferredSize(d);
				spin.setMaximumSize(d);
				// Debug.out("spin size= "+d.toString());

				this.add(nameLab);
				this.add(spin);

				attributeLookup.put(attribName, model);
				nameLookup.put(spin, attribName);
				
				spin.addChangeListener(profCardPanel);
			} 			 
		}
		SpringUtilities.makeCompactGrid(this, //parent
										numOfAttrib+2, 2,
										5, 5,  //initX, initY
										4, 4); //xPad, yPad
		  
	}
	
	public SpinnerNumberModel lookupAttributeSpinner( String name ){
		return ( (SpinnerNumberModel)attributeLookup.get(name));
	}
	
	public JLabel lookupDistanceMovedLabel() {
		JLabel l = (JLabel)attributeLookup.get(Util._elementAttribDistanceMoved);
		return ( l );
	}
	
	public String lookupAttributeNameFromSpinner( Object spinIn ){
		return ( (String)nameLookup.get(spinIn));
	}
	/**
	 * @param label
	 */
	public void setUnitNameValueLabel(String val) {
		unitNameValueLabel.setText(val);
	}

}
